package com.jinzhi.common.controller;

import com.alibaba.druid.sql.visitor.functions.If;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.jinzhi.api.util.JWTUtil;
import com.jinzhi.common.annotation.Log;
import com.jinzhi.common.base.BaseController;
import com.jinzhi.common.config.Constant;
import com.jinzhi.common.controller.util.FilePathUtil;
import com.jinzhi.common.domain.DictDO;
import com.jinzhi.common.domain.QrOssRelevancyDO;
import com.jinzhi.common.domain.Tree;
import com.jinzhi.common.service.DictService;
import com.jinzhi.common.service.QrOssRelevancyService;
import com.jinzhi.common.utils.QrCodeUtil;
import com.jinzhi.common.utils.Result;
import com.jinzhi.jzweb.domain.InfoStoreDO;
import com.jinzhi.oss.service.OssService;
import com.jinzhi.sys.domain.UserRoleDO;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.*;

/**
 * <pre>
 * 数据字典
 * </pre>
 * 
 * <small> 2018年3月23日 | Aron</small>
 */
@Controller
@RequestMapping("/common/erCodeFactory")
public class erCodeFactoryController extends BaseController {
    @Autowired
    private OssService ossService;

    @Autowired
    private QrOssRelevancyService qrOssRelevancyService;
    
    @GetMapping()
    @RequiresPermissions("common:erCodeFactory")
    public String sysDict() {
        return "common/erCodeFactory/erCodeFactory";
    }

    @PostMapping("/create")
    @ResponseBody
    public Result<String> create(String value) throws IOException {
        QrCodeUtil.createQrCode(value,"D:/upload/","二维码.jpg");
        return success();
    }


    @PostMapping("/createLogo")
    @ResponseBody
    public Result<String> createLogo(String value) throws IOException {
        value = URLDecoder.decode(value,"utf-8");
        String uuid=null;
        if(value.indexOf(".mp4")!=-1||value.indexOf(".pptx")!=-1){
            uuid=UUID.randomUUID().toString()+".jpg";
        }else {
            String [] name=value.split("/");
            if(name.length>1){
                uuid=UUID.randomUUID().toString()+name[name.length-1]+".jpg";
            }else {
                uuid=UUID.randomUUID().toString()+".jpg";
            }
        }
        QrOssRelevancyDO qrOssRelevancyDO =new QrOssRelevancyDO();
        qrOssRelevancyDO.setUrl(value);//oss文件路径
        Long userId = getUserId();
        qrOssRelevancyDO.setUserId(userId.toString());//用户id
        qrOssRelevancyDO.setCreateTime(new Date());//创建时间
        qrOssRelevancyDO.setSuffix(getfileSuffix(value)); //获取文件后缀
        boolean isn = qrOssRelevancyService.insert(qrOssRelevancyDO);
        if (isn){
            EntityWrapper<QrOssRelevancyDO> qrOssRelevancyDOEntityWrapper = new EntityWrapper<>();
            qrOssRelevancyDOEntityWrapper.orderBy("create_time",false);
            List<QrOssRelevancyDO> qrOssRelevancyDOS = qrOssRelevancyService.selectList(qrOssRelevancyDOEntityWrapper);
            QrOssRelevancyDO end = qrOssRelevancyDOS.get(0); // 取出最新一条
            value = "https://www.jz1x.com/common/erCodeFactory/getQRId?id="+end.getId();
        }else {
            return Result.fail("生成错误");
        }

        //    String logo="D://upload/logo4.png";
        //   QrCodeUtil.createQrCodeLogo(value,"D:/upload/", uuid,logo);
        String logo="/safe/java/8888/logo4.png";
        QrCodeUtil.createQrCodeLogo(value,"/upload1/", uuid,logo);

        return success(uuid);
    }



    private String getfileSuffix(String fileName){
        boolean contains = fileName.contains(".");
        String substring = "";
        if (contains){
            substring = fileName.substring(fileName.lastIndexOf(".")+1);
        }else {
            substring = "url";
        }
        return substring;
    }



    @GetMapping("/getQRId")
    public String getQRId(String id,Model model){
        System.out.println("进入二维码方法.....................");
        QrOssRelevancyDO qrOssRelevancyDO = qrOssRelevancyService.selectById(id);
        String suffix = qrOssRelevancyDO.getSuffix();
        String path = qrOssRelevancyDO.getUrl();
        model.addAttribute("path",path);
        model.addAttribute("suffix",suffix);
        return "common/erCodeFactory/QR";
    }


    @PostMapping("/createLogo1")
    @ResponseBody
    public Result<String> createLogo1(String value) throws IOException {
        String uuid=null;
            String  name=value.replaceAll("/","");
            if(name!=null){
                uuid=UUID.randomUUID().toString()+".jpg";
            }else {
                uuid=UUID.randomUUID().toString()+".jpg";
            }
        // String logo="D://upload/logo4.png";
        //   QrCodeUtil.createQrCodeLogo(value,"D:/upload/", uuid,logo);
         String logo="/safe/java/8888/logo4.png";
        QrCodeUtil.createQrCodeLogo(value,"/upload1/", uuid,logo);

        return success(uuid);
    }

    @PostMapping("/createLogos")
    @ResponseBody
    public Result<String> createLogos(String value) throws IOException {
       String  value1=value+"ewm/";

        List<String> list= FilePathUtil.getFiles(value);
        for(String li:list){
            String uuid=null;
            String uuid1=null;
            if(li.indexOf(".pptx")!=-1||li.indexOf(".docx")!=-1||li.indexOf(".doc")!=-1||li.indexOf(".ppt")!=-1||li.indexOf(".png")!=-1){
                String [] name=li.split("\t");
                if(name.length>1){
                    uuid=name[name.length-1]+".jpg";
                    uuid1=name[name.length-1];
                }else {
                    uuid=li+".jpg";
                    uuid1=li;
                }

            }else {
                String [] name=li.split("\t");
                if(name.length>1){
                    uuid=name[name.length-1];
                    uuid1=li;
                }else {
                    uuid=li;
                    uuid1=li;
                }
            }


            //  String logo="D://upload1/logo4.png";
            //  System.out.println("文件名："+uuid);
            // QrCodeUtil.createQrCodeLogo(value,value1, uuid,logo);
             String logo="/safe/java/8888/logo4.png";
              QrCodeUtil.createQrCodeLogo("https://www.jz1x.com/"+value+"/"+uuid1,value1+"/", uuid,logo);

        }
        return success();

    }



    @GetMapping("/openErCode")
    public String openErCode(Model model,String name){
        model.addAttribute("erCode", "../../../upload1/"+name);
        return "common/erCodeFactory/openErCode";
    }
}
